﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class StorageTableRepository : RepositoryBase<StorageTable>
    {
        /// <summary>
        /// 获取出入库列表
        /// </summary>
        /// <param name="type">1：出库  2：入库</param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public IQueryable<StorageTableView> GetList(int type, DateTime start, DateTime end, string name)
        {
            var result = from st in dbcontext.StorageTables
                         join pro in dbcontext.Products
                         on st.ProductId equals pro.Id
                         join inv in dbcontext.Inventories
                         on new { id1 = st.ProductId, id2 = st.StorageId } equals new { id1 = inv.ProuctId, id2 = inv.StorageId }
                         into stoEmp
                         from stoTmp in stoEmp.DefaultIfEmpty()
                         join s in dbcontext.Storage
                         on st.StorageId equals s.Id
                         into sttemp
                         from temp in sttemp.DefaultIfEmpty()
                             //join order in dbcontext.Orders on st.OrderId equals order.Id into orderEmp
                             //from order in orderEmp.DefaultIfEmpty()
                             //join cusPro in dbcontext.CustomerProducts on new { id1 = order.CustomeId.Value, id2 = st.ProductId } equals new { id1 = cusPro.CustomerID, id2 = cusPro.ProductID } into cusProEmp
                             //from cusPro in cusProEmp.DefaultIfEmpty()
                             //join inv in dbcontext.Invoices on st.InvoiceID equals inv.Id into joinDeptEmp
                             //from inv in joinDeptEmp.DefaultIfEmpty()
                         where st.Type == type && st.CreateTime >= start && st.CreateTime < end && pro.Name.Contains(name)
                         orderby st.CreateTime descending
                         select new StorageTableView
                         {
                             Id = st.Id,
                             //Status = st.Status,
                             Type = st.Type,
                             Price = pro.Price,
                             Number = pro.Number,
                             StorageId = st.StorageId,
                             OrderNumber = st.OrderNumber,
                             ProductId = st.ProductId,
                             Format = pro.Format,
                             Unit = pro.Unit,
                             Total = stoTmp != null ? stoTmp.Amount : 0,
                             Amount = st.Amount,
                             Width = pro.SizeWidth,
                             Length = pro.SizeLenth,
                             StorageName = temp != null ? temp.Name : "",
                             CreatorId = st.CreatorId,
                             CreatorName = st.CreatorName,
                             CreateTime = st.CreateTime,
                             Memo = st.Memo,
                             ProductName = pro.Name,
                             OtherType = st.OtherType,
                         };
            return result;
        }


        /// <summary>
        /// 获取出入库信息
        /// </summary>
        /// <param name="pid">商品id</param>
        /// <param name="sid">库房</param>
        /// <returns></returns>
        public IQueryable<StorageTableView> GetList(int type, Guid pid, int sid)
        {
            var result = from st in dbcontext.StorageTables
                         join pro in dbcontext.Products on st.ProductId equals pro.Id
                         //join cusPro in dbcontext.CustomerProducts on new { id1 = order.CustomeId.Value, id2 = st.ProductId } equals new { id1 = cusPro.CustomerID, id2 = cusPro.ProductID } into cusProEmp
                         //from cusPro in cusProEmp.DefaultIfEmpty()
                         //join inv in dbcontext.Invoices on st.InvoiceID equals inv.Id into joinDeptEmp
                         //from inv in joinDeptEmp.DefaultIfEmpty()
                         where st.Id == pid && (sid == 0 || st.StorageId == sid) && st.Type == 3
                         select new StorageTableView
                         {
                             Id = st.Id,
                             //Status = st.Status,
                             Type = st.Type,
                             Number = pro.Number,
                             StorageId = st.StorageId,
                             //OrderNumber = st.OrderNumber,
                             ProductId = st.ProductId,
                             Format = pro.SizeWidth + " * " + pro.SizeLenth,
                             Unit = pro.Unit,
                             Amount = st.Amount,
                             CreatorId = st.CreatorId,
                             CreatorName = st.CreatorName,
                             CreateTime = st.CreateTime,
                             //ApplyTime = st.ApplyTime,
                             //ApplyId = st.ApplyId,
                             Memo = st.Memo,
                             //InvoiceID = st.InvoiceID,
                             ProductName = pro.Name,
                             //OrderNumber = order.Number,
                             //InvoiceNumber = inv.Number,
                             //OtherID = st.OtherId,
                             OtherType = st.OtherType,
                             //CusProName = cusPro.ProductByName,
                         };
            return result;
        }


        /// <summary>
        /// 获取出入库信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StorageTableView GetSTInfo(Guid id)
        {
            StorageTableView result = (from st in dbcontext.StorageTables
                                       join pro in dbcontext.Products on st.ProductId equals pro.Id
                                       join order in dbcontext.Orders on st.OrderNumber equals order.Number into orderEmp
                                       from order in orderEmp.DefaultIfEmpty()
                                           //join cusPro in dbcontext.CustomerProducts on new { id1 = order.CustomeId.Value, id2 = st.ProductId } equals new { id1 = cusPro.CustomerID, id2 = cusPro.ProductID } into cusProEmp
                                           //from cusPro in cusProEmp.DefaultIfEmpty()
                                           //join inv in dbcontext.Invoices on st.InvoiceID equals inv.Id into joinDeptEmp
                                           //from inv in joinDeptEmp.DefaultIfEmpty()
                                       where st.Id == id
                                       select new StorageTableView
                                       {
                                           Id = st.Id,
                                           //Status = st.Status,
                                           Type = st.Type,
                                           Number = pro.Number,
                                           StorageId = st.StorageId,
                                           //OrderNumber = st.OrderNumber,
                                           ProductId = st.ProductId,
                                           Format = pro.SizeWidth + " * " + pro.SizeLenth,
                                           Unit = pro.Unit,
                                           Amount = st.Amount,
                                           CreatorId = st.CreatorId,
                                           CreatorName = st.CreatorName,
                                           CreateTime = st.CreateTime,
                                           //ApplyTime = st.ApplyTime,
                                           //ApplyId = st.ApplyId,
                                           Memo = st.Memo,
                                           //InvoiceID = st.InvoiceID,
                                           ProductName = pro.Name,
                                           OrderNumber = order.Number,
                                           //InvoiceNumber = inv.Number,
                                           //OtherID = st.OtherId,
                                           OtherType = st.OtherType,
                                           //CusProName = cusPro.ProductByName,
                                       }).FirstOrDefault();
            return result;
        }

        /// <summary>
        /// 根据关系表firstID获取订单列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fID"></param>
        /// <returns></returns>
        public IQueryable<OrderRelationView> GetOrderByRealtion(string type, Guid fID, Guid proID)
        {
            var result = from relation in dbcontext.Relations

                         join order in dbcontext.Orders
                         on relation.SecondId equals order.Id

                         join cus in dbcontext.Customers
                         on order.CustomeId equals cus.Id into cusEmp
                         from cus in cusEmp.DefaultIfEmpty()

                         join cp in dbcontext.CustomerProducts
                         on new { id1 = cus.Id, id2 = proID } equals new { id1 = cp.CustomerID, id2 = cp.ProductID } into cpEmp
                         from cp in cpEmp.DefaultIfEmpty()

                         where relation.TypeId == type && relation.FirstId == fID

                         select new OrderRelationView
                         {
                             OrderId = relation.SecondId,
                             Number = order.Number,
                             CustomeId = cus.Id,
                             CustomerName = cus.Name,
                             UserId = order.CreatorId,
                             UserName = order.CreatorName,
                             ProductName = cp.ProductByName,
                             CusProName = cp.ProductByName
                         };
            return result;
        }

    }
}
